/**
 * Created by cjd on 2016/6/7.
 */
angular.module("app").controller("org.managerList.Ctrl", ["$scope", "$http", "User", function ($scope, $http, User) {
    $scope.managerGridConfig = {
        bindingOptions: {
            selectedRowKeys: "selectedItems",
            dataSource: "managerGridConfig.dataSource"
        },
        twoWayBindingEnabled: false,
        searchPanel: {
            visible: true,
            width: 200,
            placeholder: "输入关键字进行查询"
        },
        paging: {
            pageSize: 15
        },
        groupPanel: {
            visible: true,
            emptyPanelText: "拖动列名进行分组和排序"
        },
        hoverStateEnabled: true,
        showBorders: true,
        showRowLines: true,
        rowAlternationEnabled: true,
        noDataText: "没有数据",
        columns: [{
            dataField: "userName",
            alignment: "center",
            caption: "用户名"
        }, {
            dataField: "phone",
            alignment: "center",
            caption: "手机号"
        }, {
            dataField: "certificateType",
            alignment: "center",
            width: 100,
            cellTemplate: "certificateTypeCellTemplate",
            caption: "证件类型"
        }, {
            dataField: "certificateCode",
            alignment: "center",
            caption: "证件号码"
        }, {
            dataField: "status",
            alignment: "center",
            cellTemplate: "statusCellTemplate",
            width: 60,
            caption: "状态"
        }, {
            dataField: "operation",
            alignment: "center",
            cellTemplate: "operationCellTemplate",
            caption: "操作"
        }]
    };
    $scope.orgParent = $scope.$parent.treeViewDataSource[0].orgId;
    $scope.getList = function (orgId) {
        User.getList({}, {orgId: orgId}, function (data) {
            if (data.statusCode == 0) {
                $scope.managerGridConfig.dataSource = data.data;
            }
        });
    };
    $scope.updateManagerStatus = function (managerInfo) {
        User.updateStatus({}, {userId: managerInfo.userId}, function (data) {
            if (data.statusCode == 0) {
                if (managerInfo.status == 10) {
                    managerInfo.status = 20;
                }
                else {
                    managerInfo.status = 10;
                }
                alert("更新成功!")
            }
            else {
                alert(data.statusMessage)
            }
        });
    };
    $scope.treeViewConfig = angular.copy($scope.$parent.treeViewConfig);
    $scope.treeViewConfig.onItemClick = function (e) {
        $scope.getList(e.itemData.orgId);
        $scope.orgParent = e.itemData.orgId;
        $scope.managerInfo = {};
    };
    $scope.showManagerInfo = function (managerInfo) {
        $scope.managerInfo = managerInfo;
        $scope.type = "view";
    };
    $scope.editManagerInfo = function (managerInfo) {
        $scope.managerInfo = managerInfo;
        $scope.type = "edit";
    };
    $scope.saveManager = function () {
        if($scope.managerInfo.certificateType == 1 && !/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/.test($scope.managerInfo.certificateCode)){
            alert("请输入正确的身份证号!");
            return;
        }
        // else if($scope.managerInfo.certificateType == 2 && !/^1[45][0-9]{7}|G[0-9]{8}|P[0-9]{7}|S[0-9]{7,8}|D[0-9]+$/.test($scope.managerInfo.certificateCode)){
        //     alert("请输入正确的护照号码!");
        //     var re1 = /^[a-zA-Z]{5,17}$/;
        //     var re2 = /^[a-zA-Z0-9]{5,17}$/;
        //     return;
        // }
        if(confirm("是否确定执行该操作?")){
            delete $scope.managerInfo.createTime;
            User.saveUser({}, $scope.managerInfo, function (data) {
                if (data.statusCode == 0) {
                    alert("操作成功!");
                    $scope.getList($scope.managerInfo.orgId);
                    $scope.managerInfo = {};
                }
                else{
                    alert(data.statusMessage)
                }
            });
        }
    };
    $scope.updateStatus = function (managerInfo) {
        if (confirm("是否确定执行该操作?")) {
            $scope.updateManagerStatus(managerInfo);
        }
    };
    $scope.getList($scope.$parent.treeViewDataSource[0].orgId);
    $scope.createManager = function () {
        $scope.type = "edit";
        $scope.managerInfo = {orgId:$scope.orgParent,status:10,certificateType:1,role:1};
    }
}]);